/**
 * 
 */
package com.xbang.attendance.web.action.purchase.purchaselist;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.order.PurchaseOrder;
import com.xbang.attendance.service.PurchaseOrderService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Page;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class Index extends SearchActionSupport {

	private static final long serialVersionUID = 6137752167041062321L;

	private static final Log logger = LogFactory.getLog(Index.class);

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	private List<PurchaseOrder> purchaseOrders;

	private Integer id;

	@Action("/purchase/purchaselist/index/${currPageId}")
	public String listByPage() throws Exception {
		
		return list();
	}

	@Action("/purchase/purchaselist/list")
	public String list() throws Exception {

		String hqlSentence = "FROM PurchaseOrder purchaseorder WHERE 1 = 1 ";

		if (searchComponent.getOrderParameters().isEmpty()) {
			searchComponent.addSearchElement("orderBy.purchaseorder.azkonobelDate", "DESC");
		}

		if (page != null) {
			if (currPageId.intValue() != -1 && currPageId.intValue() > 0) {
				page.setCurrentPage(currPageId);
			}
		}
		Result result = purchaseOrderService.searchByPage(hqlSentence, searchComponent, page);
		purchaseOrders = result.getItems();
		page = result.getPage();

		return "index";
	}

	@Action("/purchase/purchaselist/list/compositor")
	public String compositor() throws Exception {

		String hqlSentence = "FROM PurchaseOrder purchaseorder WHERE 1 = 1 ";

		searchComponent.resetOrderSentence();

		searchComponent.addSearchElement("orderBy.purchaseorder.orderDate", "DESC");
		searchComponent.addSearchElement("orderBy.purchaseorder.id", "ASC");
		Result result = purchaseOrderService.searchByPage(hqlSentence, searchComponent, page);
		purchaseOrders = result.getItems();
		page = result.getPage();
		return "index";
	}

	@Action("/purchase/purchaselist/list/redcompositor")
	public String redCompositor() throws Exception {

		String hqlSentence = "FROM PurchaseOrder purchaseorder WHERE 1 = 1 ";

		searchComponent.resetOrderSentence();
		searchComponent.addSearchElement("orderBy.purchaseorder.percentage", "ASC");
		searchComponent.addSearchElement("orderBy.purchaseorder.orderDate", "DESC");
		Result result = purchaseOrderService.searchByPage(hqlSentence, searchComponent, page);
		purchaseOrders = result.getItems();
		page = result.getPage();
		return "index";
	}

	public List<PurchaseOrder> getPurchaseOrders() {
		return purchaseOrders;
	}

	public void setPurchaseOrders(List<PurchaseOrder> purchaseOrders) {
		this.purchaseOrders = purchaseOrders;
	}

	@Override
	public void resetCustomerSearchCriteria() {

	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

}
